package hk.polyu;

import java.awt.event.*;
import java.awt.*;

import javax.swing.*;

public class SettingPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6335404284000071957L;
	
	// Baud rate list
	private int[] bautRates = 
		{
			110,
			300,
			1200,
			2400,
			4800,
			9600,
			19200,
			38400,
			57600,
			115200,
			230400,
			460800,
			921600
		};
	
	// many UI controls
	// used for com port setting
	JPanel comPanel;
	JLabel portLabel;
	JLabel bautLabel;
	JComboBox portList;
	JComboBox bautList;
	
	// used for warning temperature setting
	JPanel warningPanel;
	JLabel warnLabel;
	JLabel errLabel;
	JTextField warnTf;
	JTextField errTf;
	
	// the saveButton
	JButton btnSave;
	
	public SettingPanel()
	{
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		// COM port setting
		comPanel = new JPanel(new FlowLayout()); 
		comPanel.setBorder(BorderFactory.createTitledBorder("COM Port")); 
		
		//this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		portLabel = new JLabel("Port Number");
		bautLabel = new JLabel("Baudrate");
		
		portList = new JComboBox();
		portList.setPreferredSize(new Dimension(100, 30));
		// add item to list
		for(int i = 0; i <=20; i++)
		{
			portList.addItem("COM"+i);
		}
		bautList = new JComboBox();
		bautList.setPreferredSize(new Dimension(100, 30));
		// init baud rate
		for(int i = 0; i < bautRates.length; i++)
		{
			bautList.addItem(bautRates[i]);
		}
		
		comPanel.add(portLabel);
		comPanel.add(portList);
		comPanel.add(bautLabel);
		comPanel.add(bautList);
		
		comPanel.setMaximumSize(new Dimension(500, 100));
		
		this.add(comPanel);
		
		// temperature setting
		warningPanel = new JPanel(new FlowLayout()); 
		warningPanel.setBorder(BorderFactory.createTitledBorder("Temperature"));
		
		this.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		warnLabel = new JLabel("Warning Temperature");
		errLabel = new JLabel("Alert Temperature");
		
		warnTf = new JTextField();
		warnTf.setPreferredSize(new Dimension(100, 30));
		errTf = new JTextField();
		errTf.setPreferredSize(new Dimension(100, 30));
		
		warningPanel.add(warnLabel);
		warningPanel.add(warnTf);
		warningPanel.add(errLabel);
		warningPanel.add(errTf);
		
		warningPanel.setMaximumSize(new Dimension(500, 100));
		
		this.add(warningPanel);
		
		
		// the set button
		btnSave = new JButton("Save");
		this.add(btnSave);
		
		// button click event
		btnSave.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				// first update setting
				if (! SettingPanel.this.updateSettingfromUI())
				{
					JOptionPane.showMessageDialog(SettingPanel.this, "Not a valid integer number.");
					return ;
				}
				// then save setting
				
				JOptionPane.showMessageDialog(SettingPanel.this, "Setting Saved.");
				
			}
		});
		
		// load setting from DB
		// Update UI
		this.updateUIfromSetting();
		
	}
	
	// 
	private void updateUIfromSetting()
	{

	}
	
	// the reverse operation
	private boolean updateSettingfromUI()
	{

		return true;
		
	}
	
	

}
